package com.yang.jxj.controller.interceptor;

import ch.qos.logback.core.util.StringUtil;

import com.yang.jxj.common.AuthenticationException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import static com.yang.jxj.Utils.JwtUtil.getUserIdFromToken;

public class JwtInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        String userIdFromToken = getUserIdFromToken(token);
        request.setAttribute("userId", userIdFromToken);
        // 如果不是映射到方法直接通过
        if(!(handler instanceof HandlerMethod)){
            return true;
        }



        if(userIdFromToken != null){
           return  true;
        }

        return false;
    }
}
